﻿using System;
using BaseListener = CzfUDP.CzfUdpListener;
using BaseClient = CzfUDP.CzfUDPClient;
using CzfUDP;
using System.Threading;
using System.Threading.Tasks;

namespace CzfSDK
{
	public class CzfUdpListener : CzfNetListener
	{
		private BaseListener listener;
		public CzfUdpListener (int port, int mtu = 548)
		{
			this.port = port;
			listener = new BaseListener (NewThread, (time) => {
				ThreadTool.TaskDelay(time);
			}, mtu);
			listener.OnAccept += (BaseClient client) => {
				if(OnAccept != null)
					OnAccept(this, new CzfUdpClient(client));
			};
			listener.OnException += (e) => {
				if(OnException != null)
					OnException(e);
			};;
		}


		public override void Start ()
		{
			base.Start ();
			listener.StartListen (port);
		}

		void NewThread(ThreadStart action)
		{
			ThreadTool.TaskRun (() =>{
				action();
			}, TaskCreationOptions.LongRunning);
		}
	}
}

